﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Manager;
using Model;
using ExtAspNet;
namespace HealthSystem
{
    public partial class schooladmin_new : BasePage
    {
        HW_School_UserManager usermanager = new HW_School_UserManager();
        HW_SchoolManager manager = new HW_SchoolManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHidePostBackReference();
                if ((Request["act"] as string)=="edit")
                {
                    cnnname.Text = usermanager.GetById(Request["id"] as string).CNName;
                }
            }
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            try
            {
                HW_School_User user = new HW_School_User();
                if ((Request["act"] as string) == "edit")
                {
                    user= usermanager.GetById(Request["id"] as string);
                    user.CNName = cnnname.Text;
                    usermanager.ModHW_School_User(user);
                }
                else
                {
                   
                    string schoolcode = Request["schoolcode"] as string;
                    HW_School school = manager.GetById(schoolcode);

                    HW_Manger_Admin_UserManager adminmanager = new HW_Manger_Admin_UserManager();
                    HW_DistrictManager dismanager = new HW_DistrictManager();
                    var dt = dismanager.GetManager(school.DistrictCode);

                    string dis = dt.Rows[0]["XX_UserName"] as string;
                    dis = dis.Substring(0, 3);
                    HW_School_UserManager schoolusermanaer = new HW_School_UserManager();

                    string maxuser = schoolusermanaer.GetMaxUser(dis);
                    string username = string.Empty;
                    if (string.IsNullOrEmpty(maxuser))
                    {
                        username = dis + "10001";
                    }
                    else
                    {
                        username = dis + (Convert.ToInt32(maxuser.Substring(3)) + 1).ToString();
                    }

                    user.XX_UserName = username;
                    user.CNName = cnnname.Text.Trim();
                    user.SchoollCode = schoolcode;
                    user.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    schoolusermanaer.Add(user);
                }
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            catch
            {
                Alert.Show("添加学校管理员出错，请检查");
            }
        }
    }
}